package controllers;

import controllers.filters.*;
import controllers.pipes.Persistence2SenderPipe;
import controllers.pipes.Reader2ValidatorPipe;
import controllers.pipes.Validator2PersistencePipe;
import entities.Archive;
import javafx.concurrent.Task;
import view.LoadFilePane;
import view.MainWindow;

/**
 *
 * @author john
 */
public class CheckSyntacticValidationProgressTask extends Task {

    private final String filePath;
    private FileReaderFilter theFileReaderThread;
    private SyntacticValidatorFilter theValidatorThread;
    private SenderFilter theSenderThread;
    private PersistenceFilter thePersistenceThread;
    private Archive archive;

    public CheckSyntacticValidationProgressTask(String filePath) {
        this.filePath = filePath;
        theFileReaderThread = new FileReaderFilter(); // create a new thread
        theValidatorThread = new SyntacticValidatorFilter(); // create a new thread
    }
    
    public int getValidLines(){
        return theValidatorThread.getValidLinesNumber();
    }

    public String getArchiveID() {
        if (archive != null) {
            return archive.getIdMongo();
        }
        return null;
    }

    public int getValidLinesNumber() {
        return theSenderThread.getContTempVal();
    }

    public String getFilePath() {
        return filePath;
    }

    public FileReaderFilter getTheFileReaderThread() {
        return theFileReaderThread;
    }

    public SyntacticValidatorFilter getTheValidatorThread() {
        return theValidatorThread;
    }

    @Override
    protected Object call() throws Exception {//method
        try {
//          //comment from here
            Persistence2SenderPipe instance3 = Persistence2SenderPipe.getInstance();//initialize the queue to avoid thread-driven duplicity of the singleton
            Reader2ValidatorPipe instance = Reader2ValidatorPipe.getInstance();//initialize the queue to avoid thread-driven duplicity of the singleton
            Validator2PersistencePipe instance2 = Validator2PersistencePipe.getInstance();//initialize the queue to avoid thread-driven duplicity of the singleton
            theSenderThread = new SenderFilter(); // create a new thread
            thePersistenceThread = new PersistenceFilter(); // create a new thread
            theFileReaderThread.setFilePath(filePath);
            long LinesOfFile = LoadFilePane.getNumLines();
            System.out.println(LinesOfFile);
            theSenderThread.setLines(LinesOfFile);
            thePersistenceThread.setLines(LinesOfFile);
            SyntacticValidator validator = new SyntacticValidator();
            //TODO: Juan. Aquie comienza Pipes & Filter
//          Archive archive = validator.getArchive(filePath, "4fa70fae46efad42ad423cb0","4fa70fae46efad42ad423ca0");
            System.out.println("hola");
            String brandID = MainWindow.getActiveUser().getBrandId();
            String uID = MainWindow.getActiveUser().getId();
            archive = validator.getArchive(filePath, brandID,
                    uID);
            thePersistenceThread.setArchive(archive);
            theSenderThread.setArchive(archive);
            theValidatorThread.setLines(LinesOfFile);
            theFileReaderThread.start();
            new Thread(theValidatorThread).start();
            thePersistenceThread.start();
            theSenderThread.start();
        } catch (Exception crap) {
            updateMessage("Shit!");
            System.out.println(crap.getClass());
            System.out.println(crap.getMessage());
        }
        return true;
    }
}